package com.weixin.engine.filter;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 脱敏工具类
 *
 * @author wangchunhui
 * @date 2022/4/6 10:50
 */
public class DesensitizeUtil {

    private final static Logger log = LoggerFactory.getLogger(DesensitizeUtil.class);

    public static final SymmetricCrypto DES;

    static {
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
        DES = new SymmetricCrypto(SymmetricAlgorithm.DESede, key);
    }

    public static String desensitizedIdCard(String idCard) {
        return DesensitizedUtil.idCardNum(idCard, GuaranteeGlobalConstant.ID_CARD_FRONT,
                GuaranteeGlobalConstant.ID_CARD_END);
    }

    /**
     * 递归json，加密id值
     *
     * @param objJson json
     * @author wangchunhui
     * @date 2022/4/18 17:42
     */
    public static void analyzeJson(Object objJson,Integer flag) {
        if (objJson instanceof JSONArray) {
            JSONArray objArray = (JSONArray) objJson;
            for (Object object : objArray) {
                analyzeJson(object,flag);
            }
        } else if (objJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objJson;
            for (java.util.Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                String value = String.valueOf(entry.getValue());
                if (value == null){
                    break;
                }
                if (key.equals(GuaranteeGlobalConstant.DECRYPT_FIELD) ){
                    if (flag.equals(GuaranteeGlobalConstant.ENCRYPT_FLAG)&& value.matches("[0-9]+")) {
                        jsonObject.put(key, encrypt(value));
                        continue;
                    }
                    if (flag.equals(GuaranteeGlobalConstant.DECRYPT_FLAG) ) {
                        jsonObject.put(key, decrypt(value));
                        continue;
                    }
                }
                Object object = jsonObject.get(key);
                //如果得到的是数组
                if (object instanceof JSONArray) {
                    JSONArray objArray = (JSONArray) object;
                    analyzeJson(objArray,flag);
                } else if (object instanceof JSONObject) {
                    analyzeJson(object,flag);
                }
            }
        }
    }

    /**
     * 直接解密
     *
     * @param str str
     * @return java.lang.String
     * @author wangchunhui
     * @date 2022/4/28 21:10
     */
    public static String decrypt(String str) {
        return DES.decryptStr(str);
    }

    /**
     * 直接加密
     *
     * @param str str
     * @return java.lang.String
     * @author wangchunhui
     * @date 2022/4/28 21:10
     */
    public static String encrypt(String str) {
        return DES.encryptHex(str);
    }

    public static void main(String[] args) {
        String encrypt = encrypt("123");
        System.out.println(encrypt);
        String decrypt = decrypt(encrypt);
        System.out.println(decrypt);
    }
}
